package com.baizhi.api.controller;

import com.alibaba.druid.sql.visitor.functions.Bin;
import com.baizhi.OrderException;
import com.baizhi.api.service.CategoryService;
import com.baizhi.entity.BzAdmin;
import com.baizhi.entity.PmsCategory;
import com.baizhi.interfaces.AddGroup;
import com.baizhi.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("菜单查询的接口类")
@Validated
@Slf4j
@RestController
@RequestMapping("/api/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("根据id查询二级三级菜单")
    @GetMapping("/getCategoryById")
    public R getCategoryById(@RequestParam("categoryId") @NotNull Long categoryId){

        List<PmsCategory> list = null;

        int i = 1/0;

        try {
            list = categoryService.getCategoryById(categoryId);
        }catch (Exception e){
            log.error("一级类别id:{}不存在",categoryId);
            throw new OrderException();
        }
        log.info("查询到id：{}的类别数据",categoryId);
        return R.ok().put("data",list);
    }

    /**
     * BindingResult Spring会自动的将所有的参数校验的结果封装到result中 不会抛异常
     */
    @PostMapping("addCategory")
    public R addCategory(@Validated(AddGroup.class) PmsCategory category){

        System.out.println(category);
        return R.ok();
    }

}
